package com.vcixp.common.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.nutz.log.Log;
import org.nutz.log.Logs;

public class  PropertyUtils{

	private static Log log = Logs.get();
	private static String FILE = "/application.properties";
    private static Properties p = new Properties();
    
    static {
    	InputStream is = PropertyUtils.class.getResourceAsStream(FILE);
        try {
            p.load(is);
            is.close();
        } catch (IOException e) {
            log.error(e);
        }
    }
    public static String getProperty(String key) {
        return p.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return p.getProperty(key, defaultValue);
    }
    
    public static void setProperty(String key, String value) {
    	p.setProperty(key, value);
    	try {
    		OutputStream fos = new FileOutputStream(PropertyUtils.class.getResource(FILE).getPath());
    		p.store(fos, "Update '" + key + "' value");
    		fos.close();
    	}catch (Exception e) {
    		log.error(e);
		}
    }
}
